package engine.newUi
{
	import engine.control.NxTablePageTurner;
	import engine.data.ModelLocator;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	
	import flash.events.MouseEvent;
	
	import newx.com.config.StaticIni;
	import newx.com.control.window.NxWindowEvent;

	/**
	 *帮派战查看逻辑 
	 * @author liuel
	 * 
	 */	
	public class FactionBattleLookLogic
	{
		private const COLOR_TABLE	:Array	=	["","#ffffff'>","#1eff00'>","#0070dd'>","#a335ee'>","#ff8000'>","#e5cc80'>","#ffff00'>","#ff0000'>"];
		private static var _self		:FactionBattleLookLogic;
		private var _ui				:FactionBattleLookUI;
		private var _model				:ModelLocator;
		//管理翻页
		private var _pageTurner		:NxTablePageTurner;
		//自己帮派名字
		private var _selfFactionName	:String;
		//保存排行榜的数组
		private var _rankVector		:Vector.<FactionRankVo>;
		public function FactionBattleLookLogic()
		{
		}
		public static function getInstance():FactionBattleLookLogic
		{
			if(_self == null)
			{
				_self = new FactionBattleLookLogic();
			}
			return _self;
		}
		public function Init() : void
		{
			_rankVector	= new Vector.<FactionRankVo>();
			_ui			= FactionBattleLookUI.getInstance();
			_model		= ModelLocator.getInstance();
			//接受数据打开节界面
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_GUILD_FIGHT_SIGNUP_LIST),getInfo);
			_pageTurner	= new NxTablePageTurner(_ui.upPage,_ui.nextPage,_ui.nxTabel,_ui.pageLabel);
			_ui.nxTabel.dataProvider =null;
		}
		public function	BeforeShow():void
		{
			_rankVector	= new Vector.<FactionRankVo>();
//			_selfFactionName = _model.scene.Role.getPlayerGuild();
//			getInfo();
			addEvent();
			_ui.infoLabel.htmlText =StaticIni.getSwfLangStr("GuildUIStr_28");
		}
		public function	AfterHide():void
		{
			_rankVector	= new Vector.<FactionRankVo>();
			removeEvent();
		}
		private function addEvent():void
		{
			_pageTurner		.addEventListener();
			_ui.windowBG	.addEventListener(NxWindowEvent.WINDOW_CLOSE	,onClickClose);
			_ui.okBtn		.addEventListener(MouseEvent.CLICK,		onClickBtn);
		}
		private function removeEvent():void
		{
			_pageTurner		.removeEventListener();
			_ui.windowBG	.removeEventListener(NxWindowEvent.WINDOW_CLOSE	,onClickClose);
			_ui.okBtn		.removeEventListener(MouseEvent.CLICK,		onClickBtn);
		}
		private function getInfo(e:CustomMsgEvent =null):void
		{
			_selfFactionName = _model.scene.Role.getPlayerGuild();
			var factionArr:Array =[];
			factionArr =e.pList;
			saveRank(factionArr);
			showLineInfo();
			if(_ui.created == false)
			{
				_ui.showOrHide();
			}
		}
		private function saveRank(arr:Array):void
		{
			_rankVector	= new Vector.<FactionRankVo>();
			var index:int =1;
			for(var i:int=0;i<arr.length;i+=4)
			{
				var vo :FactionRankVo=new FactionRankVo(index,arr[i],arr[i+1],arr[i+2],arr[i+3]);
				if(_rankVector.indexOf(vo.name) == -1)
				{
					_rankVector.push(vo);
				}
				index++;
			}
			//按排名排序
			taxis(_rankVector);
		}
		private function showLineInfo():void
		{
			_ui.nxTabel.dataProvider =[];
			var itemArr:Array =[];
			var colcorID:int =5; 
			for(var i:String in _rankVector)
			{
				//是自己的帮派设置字体颜色
				if(_rankVector[i].name ==_selfFactionName)
				{
					itemArr.push( [{label:link_color(String(int(i)+1),colcorID)},
						{label:link_color(_rankVector[i].name,colcorID)},{label:link_color(_rankVector[i].level.toString(),colcorID)},
						{label:link_color(_rankVector[i].todaynum.toString(),colcorID)},{label:link_color(_rankVector[i].totalnum.toString(),colcorID)}]);
				}
				else
				{
				itemArr.push( [{label:String(int(i)+1)},{label:_rankVector[i].name},{label:_rankVector[i].level},{label:_rankVector[i].todaynum},{label:_rankVector[i].totalnum}]);
				}
			}	
			_ui.nxTabel.dataProvider =itemArr;
		}
		/**
		 * 排序函数 
		 * @param vector
		 * 
		 */		
		private function taxis(vector:Vector.<FactionRankVo>):void
		{
			vector = vector.sort(compareFunc);
		}
		private function compareFunc(vo1:FactionRankVo,vo2:FactionRankVo):int
		{
			return vo2.totalnum-vo1.totalnum;
		}
		private function link_color(str:String,color:int):String
		{
			if(color < 1 || color >8)
			{
				color	=	1;
			}
			return ("<font color ='"+COLOR_TABLE[color]+str+"</font>");
		}
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		private function onClickClose(e:NxWindowEvent):void
		{
			_ui.hide();
		}
		private function onClickBtn(e:MouseEvent):void
		{
			_ui.hide();
		}
	}
}
class FactionRankVo
{
	public var index:int;		//排名
	public var name:String; 	//帮派名字
	public var level:int;		//等级
	public var todaynum:int;	//今天贡献
	public var totalnum:int;	//本周贡献
	public function FactionRankVo(index:int,name:String,level:int,todatnum:int,totalnum:int)
	{
		this.index		= index;
		this.name 		= name;
		this.level		= level;
		this.todaynum 	= todatnum;
		this.totalnum 	= totalnum;
	}
	
}
